package com.c.ctgproject.ui.collect

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.c.ctgproject.R
import com.c.ctgproject.adapter.ImageCommonRvV2Adapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryDetailBinding
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.common.CarInfoLayout
import com.c.ctgproject.wiget.dialog.ShowImgVideoFragment
import org.json.JSONObject

/**
 *作者:LiBW
 *创建日期: 2021/9/16
 *描述: 电池详情 页面 入库单详情 页面
 */
class BatteryDetailActivity : BaseActivityV2() {
    private val orderId by lazy { intent.getIntExtra(MyConstants.ORDER_ID, -1).handlerNull() }
    private val mBinding by lazy { ActivityBatteryDetailBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root

    override fun event() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryDetail(orderId), MyObserverHandlerLoading(this, loading) {
            MyLog.d("电池详情回调:$it")
            val jsonObject = JSONObject(it)
            val data = jsonObject.getJSONObject("data")
            mBinding.mContainer1.addView(CarInfoLayout(this, "入库单号", data.optString("orderNo", "暂无数据").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "电池名称", data.optString("batteryName", "暂无数据").noDataHandler()))
            val batteryType = when (data.optInt("batteryType", -1)) {
                1 -> "电池包"
                2 -> "模组"
                3 -> "电芯"
                else -> "暂无数据"
            }
            mBinding.mContainer1.addView(CarInfoLayout(this, "电池类型", batteryType))
            mBinding.mContainer1.addView(CarInfoLayout(this, "数量", data.optString("count", "0").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "重量KG", data.optString("weight", "0").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "库位", data.optString("stockPosi", "").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "模组个数", data.optString("moduleCount", "0").noDataHandler()))
//            mBinding.mContainer1.addView(CarInfoLayout(this, "重量KG", data.optDouble("weight", 0.0).noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "容量Ah", data.optString("capacityAh", "0").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "电压V", data.optString("voltage", "0").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "容量KWh", data.optString("capacityKwh", "0").noDataHandler()))


            mBinding.mContainer1.addView(CarInfoLayout(this, "电池来源", data.optString("batterySource", "暂无数据").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "电池编码", data.optString("batteryCode", "暂无数据").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "电池自编码", data.optString("batterySelfCode", "暂无数据").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "路桥费", data.optString("toll", "暂无数据").noDataHandler()))
            mBinding.mContainer1.addView(CarInfoLayout(this, "备注", data.optString("remark", "暂无数据").noDataHandler()))
            //用来检测未过磅的单子肯定是没有过磅信息的 但是测试非要展示空数据 那就展示吧
//            if (data.optInt("status", -1) == 1) {
            mBinding.mContainer2.addView(CarInfoLayout(this, "磅单号", data.optString("poundNo", "暂无数据").noDataHandler()))
            mBinding.mContainer2.addView(CarInfoLayout(this, "过磅人", data.optString("poundPersonName", "暂无数据").noDataHandler()))
            mBinding.mContainer2.addView(CarInfoLayout(this, "过磅时间", data.optString("poundTime", "暂无数据").noDataHandler()))
            mBinding.mContainer2.addView(CarInfoLayout(this, "入库重量KG", data.optString("poundWeight", "暂无数据").noDataHandler()))
            mBinding.mContainer2.addView(CarInfoLayout(this, "采购磅单号", data.optString("purchasePoundNo", "暂无数据").noDataHandler()))
            mBinding.mContainer2.addView(CarInfoLayout(this, "采购重量KG", data.optString("purchaseWeight", "暂无数据").noDataHandler()))
            mBinding.mContainer2.addView(CarInfoLayout(this, "磅单备注", data.optString("poundRemark", "暂无数据").noDataHandler()))
            val image = data.optString("poundPicture", "").handlerNull()
            if (image.isNotEmpty()) {
                mBinding.mImageViewTV.visibility = View.VISIBLE
                mBinding.mImageView.visibility = View.VISIBLE
                Glide.with(this)
                    .load(image)
                    .error(R.mipmap.app_icon)
                    .into(mBinding.mImageView)
                mBinding.mImageView.setOnClickListener {
                    ShowImgVideoFragment(image).show(supportFragmentManager, "")
                }
            }
            val picUrls = data.optJSONArray("picUrls")
            picUrls?.let {
                val secondImage = ArrayList<String>()
                repeat(picUrls.length()) {
                    secondImage.add(picUrls.getString(it))
                }
                if (secondImage.isNotEmpty()) {
                    mBinding.mSecondImageTV.visibility = View.VISIBLE
                    mBinding.mSecondImageRV.visibility = View.VISIBLE
                    mBinding.mSecondImageRV.adapter = ImageCommonRvV2Adapter(this, secondImage)
                    mBinding.mSecondImageRV.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
                }
            }
            val imgUrls  = data.optJSONArray("imgUrls")
            imgUrls?.let {
                val firstImage = ArrayList<String>()
                repeat(imgUrls.length()) {
                    firstImage.add(imgUrls.getString(it))
                }
                if (firstImage.isNotEmpty()) {
                    mBinding.mFirstImageTV.visibility = View.VISIBLE
                    mBinding.mFirstImageRV.visibility = View.VISIBLE
                    mBinding.mFirstImageRV.adapter = ImageCommonRvV2Adapter(this, firstImage)
                    mBinding.mFirstImageRV.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
                }
            }
//            } else {
//                mBinding.mPoundLL.visibility = View.GONE
//
//            }
            mBinding.mContainer3.addView(CarInfoLayout(this, "开单时间", data.optString("createTime", "暂无数据").noDataHandler()))
            mBinding.mContainer3.addView(CarInfoLayout(this, "开单人", data.optString("createPersonName", "暂无数据").noDataHandler()))
            mBinding.mContainer3.addView(CarInfoLayout(this, "送货司机", data.optString("driverName", "暂无数据").noDataHandler()))
            mBinding.mContainer3.addView(CarInfoLayout(this, "送货车牌", data.optString("driverPlate", "暂无数据").noDataHandler()))
        })
    }

    override fun click() {

    }
}